package org.example.entity;

import java.util.*;
import java.util.random.RandomGenerator;
import java.util.stream.Collectors;

public class CollectTester {

    private static String[] names = {"立诚袁", "嘉懿令狐", "果梁", "修洁曹", "立辉毛", "思远孔", "烨霖戴", "博文孙", "思远崔", "鹏煊程",};
    private static String[] sexs = {"男", "女"};

    public static void main(String[] args) {
        // Arrays.stream(names).forEach(e -> System.out.print("\"" + e + "\","));
        RandomGenerator random = RandomGenerator.getDefault();

        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i + 1);
            user.setSex(sexs[random.nextInt(2)]);
            user.setName(names[i]);
            user.setAge(random.nextInt(10));
            list.add(user);
        }
        list.stream().forEach(System.out::println);
        System.out.println();

        // 1 toMap 测试
        // 1.1 直接转 map , key不能重复
        Map<Integer, User> map = list.stream().collect(Collectors.toMap(User::getId, v -> v));
        System.out.println("map = " + map);

        // 1.2 直接转 map , key 重复的时候,怎么聚合
        Map<String, String> mapNames = list.stream().collect(Collectors.toMap(User::getSex, User::getName, (k1, k2) -> k1 + "," + k2));
        System.out.println("mapNames = " + mapNames);
        // 1.3 返回自定义类型
        Map<String, String> treeMap = list.stream().collect(Collectors.toMap(User::getSex, User::getName, (k1, k2) -> k1 + "," + k2, TreeMap::new));
        System.out.println("treeMap = " + treeMap);

        // 2 joining测试
        String joining = list.stream().map(User::getName).collect(Collectors.joining(",", "@", "-"));
        System.out.println("joining = " + joining);

        // 3. groupingBy 测试
        // 3.1 根据姓别分组
        Map<String, List<User>> groupingBy = list.stream().collect(Collectors.groupingBy(User::getSex));
        System.out.println("groupingBy = " + groupingBy);
        // 3.2 根据属性聚合分组
        Map<String, List<String>> groupingBy2 = list.stream().collect(Collectors.groupingBy(User::getSex, Collectors.mapping(User::getName, Collectors.toList())));
        System.out.println("groupingBy2 = " + groupingBy2);
        // 3.3 只是返回类型不一样
        Map<String, List<String>> groupingBy3 = list.stream().collect(Collectors.groupingBy(User::getSex, TreeMap::new, Collectors.mapping(User::getName, Collectors.toList())));
        System.out.println("groupingBy3 = " + groupingBy3);

        // 4 summarizing 汇总
        IntSummaryStatistics sum = list.stream().collect(Collectors.summarizingInt(User::getAge));
        System.out.println("sum = " + sum);

        // 5. reducing maxBy minBy 不写了,用stream就好

        // 6. partitioningBy 类似于filter效果,不过只分组一次,实际项目很常用
        Map<Boolean, List<User>> partitionBy = list.stream().collect(Collectors.partitioningBy(e -> "男".equals(e.getSex())));
        System.out.println("true = " + partitionBy.get(true));
        System.out.println("false = " + partitionBy.get(false));

        // 7. collectingAndThen 基本用不上
    }
}
